<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="Connect.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
<script language="javascript" type="text/javascript">
	function viewAuctionsClicked(button) {
		var myForm = document.createElement("form");
		myForm.setAttribute('method', "post");
		myForm.setAttribute('action', "view_auctions.jsp");
		var data = document.createElement("input");
		data.setAttribute('type', "hidden");
		data.setAttribute('name', "id");
		data.setAttribute('value', button.title);
		myForm.appendChild(data);
		javascript: myForm.submit();
	}
</script>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>Best Sellers</h1>
		<hr></hr>
		<%
			// Getting item information.
			java.sql.Connection conn = null;
			try {
				// Connect to the database.
				Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", ConnectionInfo.myUserID());
				sysprops.put("password", ConnectionInfo.myPassword());
				conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

				// Display a table of item information.
				java.sql.Statement statement = conn.createStatement();
				java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM items ORDER BY copies_sold DESC");
				java.sql.ResultSetMetaData columns = rs.getMetaData();
				while (rs.next()) {
		%>
		<table class="center item-table">
			<tr>
				<th class="left-text padding-5px"><a id=<%=" " + rs.getString("id")%>>Item Id: <%=" " + rs.getString("id")%></a></th>
				<th></th>
			</tr>
			<tr>
				<%@ page import="java.io.File"%>
				<%
					String imagePath = application.getRealPath("item_images") + "\\" + rs.getString("id") + ".png";
							File file = new File(imagePath);
							if (!file.exists()) {
								//imagePath = application.getRealPath("item_images") + "\\" + "0" + ".png";
								imagePath = "images/logo.png";
							} else {
								// Runtime correctioin. Mostly has to do with local servers.
								imagePath = imagePath.substring(imagePath.indexOf("item_images"));
							}
				%>
				<td style="vertical-align: top" width="128" height="128"><img src="<%=imagePath%>" alt="item image" width="128" height="128"></td>
				<td>
					<table class="inner-table">
						<tr>
							<th>Name:</th>
							<td><%=" " + rs.getString("name")%></td>
						</tr>
						<tr>
							<th>Year:</th>
							<td><%=" " + rs.getString("year")%></td>
						</tr>
						<tr>
							<th>Type:</th>
							<td><%=" " + rs.getString("type")%></td>
						</tr>
						<tr>
							<th>Copies Available:</th>
							<td><%=" " + rs.getString("copies_available")%></td>
						</tr>
						<tr>
							<th>Copies Sold:</th>
							<td><%=" " + rs.getString("copies_sold")%></td>
						</tr>
						<tr>
							<th>Description:</th>
							<td><%=" " + rs.getString("description")%></td>
						</tr>
						<tr>
							<td></td>
							<td><table class="right">
									<tr>
										<%
											String user_type = (String) session.getAttribute("user_type");
													out.println("<td><a href=\"#\" class=\"right button\" title=" + rs.getString(columns.getColumnName(1))
															+ " onclick=\"return viewAuctionsClicked(this)\">View Auctions</a></td>");
										%>
									</tr>
								</table></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
		<br />
		<%
			}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					conn.close();
				} catch (Exception ee) {
				}
			}
		%>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>